package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.shoppingCartMapper;
import com.sky.service.shoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class shoppingCartServiceImpl implements shoppingCartService {
    @Autowired
    private shoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Transactional
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
         ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> sList = shoppingCartMapper.list(shoppingCart);
        //此时购物车如果没有数据
        if(CollectionUtils.isEmpty(sList)){
            //直接添加数据 但是要补全信息先
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            if(shoppingCartDTO.getDishId()!=null){
                DishVO dish = dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            }
            else{
                SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
            }
            shoppingCartMapper.add(shoppingCart);
        }
        else {
            ShoppingCart cart = sList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.listByUserId(BaseContext.getCurrentId());
    }

    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //先查看购物车商品数量如果等于1就删除记录不然的话就是更新记录number-1还有口味的问题
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> sList = shoppingCartMapper.list(shoppingCart);
        if(!CollectionUtils.isEmpty(sList)){
            for(ShoppingCart cart : sList){
                if(cart.getNumber()==1){
                    shoppingCartMapper.deleteById(cart.getId());
                }
                else{
                    cart.setNumber(cart.getNumber()-1);
                    if(shoppingCartDTO.getDishFlavor()!=null){
                        cart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                    }
                    shoppingCartMapper.update(cart);
                }
            }
        }
    }

    @Override
    public void deleteAll() {
        shoppingCartMapper.deleteAll(BaseContext.getCurrentId());
    }
}
